package com.example.demo.common;

import com.example.demo.game.GetUserLoginMatch;
import com.example.demo.game.Matcher;
import com.example.demo.game.ReqMatch;
import com.example.demo.game.RespMatch;
import com.example.demo.model.Userinfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class TextTatch extends TextWebSocketHandler {
    private ObjectMapper objectMapper =new ObjectMapper();

    @Autowired
    private GetUserLoginMatch getUserLogin;

    @Autowired
    private Matcher matcher;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        Userinfo userinfo= (Userinfo) session.getAttributes().get(AppValues.USER_KEY);//获取到当前登录用户的session信息
        if(userinfo==null) {
            RespMatch respMatch =new RespMatch();
            respMatch.setOk(false);
            respMatch.setMessage("用户未登录");
            respMatch.setMessage("stopMatch");
            //将响应对象转换为json,后发送,因为send中必须为TextMessage格式,所以要再加一层包装
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
            return;
        }
        WebSocketSession socketSession =getUserLogin.getFromGameHall(userinfo.getUserid());
        if(socketSession!=null||getUserLogin.getFromGameGame(userinfo.getUserid())!=null) {//这里表示已经在其他地方登录了,在这里采用登录了就不可以再登录的方法,也可以使用挤下线的方式
            RespMatch respMatch =new RespMatch();
            respMatch.setOk(true);
            respMatch.setMessage("noshuang");
            respMatch.setReason("禁止多开");
            //将已经登录的信息返回去并关闭连接
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
            //关闭连接后会调用关闭连接的方法
            socketSession.close();
        }
        getUserLogin.enterGameHall(userinfo.getUserid(),session);

        System.out.println("成功设置session");
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        Userinfo userinfo= (Userinfo) session.getAttributes().get(AppValues.USER_KEY);
        String result=message.getPayload();
        //将前端传来的json格式转换为java对象(匹配请求或取消匹配请求)
        ReqMatch reqMatch= objectMapper.readValue(result,ReqMatch.class);
        RespMatch respMatch =new RespMatch();
        System.out.println(reqMatch.getMessage());
        if(reqMatch.getMessage().equals("stopmatch")) {// 取消匹配的请求
            //从匹配队列中取消
            matcher.removeMather(userinfo);
            respMatch.setOk(true);
            respMatch.setMessage("stopmatch");
            System.out.println("取消匹配");
        }else if(reqMatch.getMessage().equals("statrmatch")) {//申请匹配的请求
            //加入匹配队列
            matcher.addMatcher(userinfo);
            respMatch.setOk(true);
            respMatch.setMessage("statrmatch");
            System.out.println("开始匹配");
        }else if(reqMatch.getMessage().equals("machine")) {//申请人机对战的请求
            //这里......
            respMatch.setOk(true);
            respMatch.setMessage("machine");
            System.out.println("开始人机匹配");
        }else{
            respMatch.setOk(false);
            respMatch.setReason("请求数据错误");
        }
        session.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        Userinfo userinfo= (Userinfo) session.getAttributes().get(AppValues.USER_KEY);//获取到当前登录用户的session信息
        if(userinfo==null) {
            RespMatch respMatch =new RespMatch();
            respMatch.setOk(false);
            respMatch.setMessage("用户未登录");
            respMatch.setMessage("stopMatch");
            //将响应对象转换为json,后发送,因为send中必须为TextMessage格式,所以要再加一层包装
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
        }
        WebSocketSession socketSession =getUserLogin.getFromGameHall(userinfo.getUserid());
        if(socketSession==session) {
            getUserLogin.exitGameHall(userinfo.getUserid());
            System.out.println("session错误");
        }

    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        Userinfo userinfo= (Userinfo) session.getAttributes().get(AppValues.USER_KEY);//获取到当前登录用户的session信息
        if(userinfo==null) {
            RespMatch respMatch =new RespMatch();
            respMatch.setOk(false);
            respMatch.setMessage("用户未登录");
            respMatch.setMessage("stopMatch");
            //将响应对象转换为json,后发送,因为send中必须为TextMessage格式,所以要再加一层包装
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(respMatch)));
        }
        //处理玩家多开操作,判定是否存有玩家信息,有则代表已经登录,禁止再登录
        WebSocketSession socketSession =getUserLogin.getFromGameHall(userinfo.getUserid());
        if(socketSession==session) {
            getUserLogin.exitGameHall(userinfo.getUserid());
            System.out.println("成功销毁session");
        }


    }
}
